package com.toommi.refuse.service.impl;

import com.toommi.refuse.common.util.DateUtil;
import com.toommi.refuse.entity.AppUserSubStatistics;
import com.toommi.refuse.entity.AppUserSubStatisticsExample;
import com.toommi.refuse.mapper.AppUserSubStatisticsMapper;
import com.toommi.refuse.model.AppUserWeightModel;
import com.toommi.refuse.service.AppUserSubStatisticsService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yangg
 * @since 2019-08-02 16:27
 */
@Service
@AllArgsConstructor
public class AppuserSubStatisticsServiceImpl implements AppUserSubStatisticsService {

    private AppUserSubStatisticsMapper subStatisticsMapper;


    @Override
    public void addStatistics(long appUserId, double weight, long subRefuseTypeId) {

        String nowStr = DateUtil.formatTimestampToString(System.currentTimeMillis(), DateUtil.DATE_FORMAT_YMD);

        AppUserSubStatisticsExample example = new AppUserSubStatisticsExample();
        example.createCriteria().andAppUserIdEqualTo(appUserId)
                .andDateEqualTo(nowStr)
                .andSubRefuseTypeEqualTo(subRefuseTypeId);

        AppUserSubStatistics statistics = subStatisticsMapper.selectOneByExample(example);
        if (statistics == null) {
            statistics = new AppUserSubStatistics();
            statistics.setAppUserId(appUserId);
            statistics.setDate(nowStr);
            statistics.setSubRefuseType(subRefuseTypeId);
            statistics.setWeight(weight);

            subStatisticsMapper.insert(statistics);
        } else {

            statistics.setWeight(new BigDecimal(statistics.getWeight()).add(new BigDecimal(weight)).doubleValue());
            subStatisticsMapper.updateByPrimaryKey(statistics);
        }

    }

    @Override
    public AppUserWeightModel getStatistics(long appUserId, String date) {

        if (StringUtils.isEmpty(date)) {
            date = DateUtil.formatTimestampToString(System.currentTimeMillis(), DateUtil.DATE_FORMAT_YMD);
        }

        AppUserSubStatisticsExample example = new AppUserSubStatisticsExample();
        example.createCriteria().andAppUserIdEqualTo(appUserId)
                .andDateEqualTo(date);
        AppUserWeightModel weightModel = new AppUserWeightModel();
        weightModel.setTotal(0.0);
        List<AppUserSubStatistics> subStatisticsList = subStatisticsMapper.selectByExample(example);

        if (subStatisticsList == null || subStatisticsList.isEmpty()) {
            weightModel.setTotal(0.0);
            weightModel.setList(new ArrayList<>());

        } else {
            weightModel.setList(subStatisticsList);
            for (AppUserSubStatistics subStatistics : subStatisticsList) {
                weightModel.setTotal(new BigDecimal(weightModel.getTotal())
                        .add(new BigDecimal(subStatistics.getWeight())).doubleValue());
            }
        }

        return weightModel;
    }

    @Override
    public AppUserWeightModel getMonthStatistics(long appUserId, String date) {

        AppUserSubStatisticsExample example = new AppUserSubStatisticsExample();
        example.createCriteria().andDateLike("%" + date + "%")
                .andAppUserIdEqualTo(appUserId);

        List<AppUserSubStatistics> appUserSubStatistics = subStatisticsMapper.selectByExample(example);

        AppUserWeightModel model = new AppUserWeightModel();

        if (appUserSubStatistics == null || appUserSubStatistics.isEmpty()) {
            model.setTotal(0.0);
            model.setMonthTotal(0.0);
            model.setList(new ArrayList<>());
            return model;
        }

        Map<String, BigDecimal> dayStatistics = new HashMap<>();
        BigDecimal monthTotal = new BigDecimal(0.0);

        for (AppUserSubStatistics subStatistics : appUserSubStatistics) {
            //计算当月的以及每天的
            if (dayStatistics.containsKey(subStatistics.getDate())) {
                //该日期已有记录，累加
                dayStatistics.put(subStatistics.getDate(),
                        dayStatistics.get(subStatistics.getDate())
                                .add(new BigDecimal(subStatistics.getWeight())));


            } else {
                dayStatistics.put(subStatistics.getDate(),
                        new BigDecimal(subStatistics.getWeight()));
            }
            monthTotal = monthTotal.add(new BigDecimal(subStatistics.getWeight()));
        }

        String dayNow = DateUtil.formatTimestampToString(System.currentTimeMillis(), DateUtil.DATE_FORMAT_YMD);

        BigDecimal weightToday = dayStatistics.get(dayNow) == null ? new BigDecimal(0.0) : dayStatistics.get(dayNow);

        //每天的称重数据
        List<AppUserSubStatistics> subStatisticsList = new ArrayList<>(dayStatistics.size());
        for (String key : dayStatistics.keySet()) {
            AppUserSubStatistics subStatistics = new AppUserSubStatistics();
            subStatistics.setDate(key);
            subStatistics.setWeight(dayStatistics.get(key).doubleValue());
            subStatisticsList.add(subStatistics);
        }

        model.setTotal(weightToday.doubleValue());
        model.setMonthTotal(monthTotal.doubleValue());
        model.setList(subStatisticsList);

        return model;
    }
}
